package com.spowin.Mansion88;

import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Util {
	private static Log logger = LogFactory.getLog(Util.class);
	public static MansionDSpojo getMainsionDSpojo(String value) {
		String result[] = value.split(",");

		MansionDSpojo pojo = new MansionDSpojo();

		pojo.setUnknownAttribute1("unknown");
		if (result[1].equals("''"))
			pojo.setLeageNameShort("none");
		else
			pojo.setLeageNameShort(result[1].substring(1).split("'")[0]);
		if (result[2].equals("''"))
			pojo.setLeageNameFull("none");
		else
			pojo.setLeageNameFull(result[2].substring(1).split("'")[0]);
		if (result[3].equals("''"))
			pojo.setUnknownAttribute2("none");
		else
			pojo.setUnknownAttribute2("unknown");
		if (result[4].equals("''"))
			pojo.setUnknownAttribute3("none");
		else
			pojo.setUnknownAttribute3("unknown");
		if (result[5].equals("''"))
			pojo.setGameDate("none");
		else
			pojo.setGameDate(result[5].substring(1).split("'")[0]);
		if (result[6].equals("''"))
			pojo.setGameTime("none");
		else
			pojo.setGameTime(result[6].substring(1).split("'")[0]);
		if (result[7].equals("''"))
			pojo.setRangQiuFlag("none");
		else
			pojo.setRangQiuFlag(result[7].substring(1).split("'")[0]);
		if (result[8].equals("''"))
			pojo.setHomeTeamName("none");
		else
			pojo.setHomeTeamName(result[8].substring(1).split("'")[0]);
		if (result[9].equals("''"))
			pojo.setCustTeamName("none");
		else
			pojo.setCustTeamName(result[9].substring(1).split("'")[0]);
		if (result[10].equals("''"))
			pojo.setInRangQiuPanKouFullTime("none");
		else
			pojo
					.setInRangQiuPanKouFullTime(result[10].substring(1).split(
							"'")[0]);
		if (result[11].equals("''"))
			pojo.setHkRangQiuPanKouFullTime("none");
		else
			pojo
					.setHkRangQiuPanKouFullTime(result[11].substring(1).split(
							"'")[0]);
		if (result[12].equals("''"))
			pojo.setShangPanFullTime("none");
		else
			pojo.setShangPanFullTime(result[12].substring(1).split("'")[0]);
		if (result[13].equals("''"))
			pojo.setXiaPanFullTime("none");
		else
			pojo.setXiaPanFullTime(result[13].substring(1).split("'")[0]);
		if (result[14].equals("''"))
			pojo.setInDaXiaoPanKouFullTime("none");
		else
			pojo
					.setInDaXiaoPanKouFullTime(result[14].substring(1).split(
							"'")[0]);
		if (result[15].equals("''"))
			pojo.setHkDaXiaoPanKouFullTime("none");
		else
			pojo
					.setHkDaXiaoPanKouFullTime(result[15].substring(1).split(
							"'")[0]);
		if (result[16].equals("''"))
			pojo.setDaQiuFullTime("none");
		else
			pojo.setDaQiuFullTime(result[16].substring(1).split("'")[0]);
		if (result[17].equals("''"))
			pojo.setXiaoQiuFullTime("none");
		else
			pojo.setXiaoQiuFullTime(result[17].substring(1).split("'")[0]);
		if (result[18].equals("''"))
			pojo.setHomeTeamWinFullTime("none");
		else
			pojo.setHomeTeamWinFullTime(result[18].substring(1).split("'")[0]);
		if (result[19].equals("''"))
			pojo.setHomeTeamLoseFullTime("none");
		else
			pojo.setHomeTeamLoseFullTime(result[19].substring(1).split("'")[0]);
		if (result[20].equals("''"))
			pojo.setHomeTeamDrawFullTime("none");
		else
			pojo.setHomeTeamDrawFullTime(result[20].substring(1).split("'")[0]);
		if (result[21].equals("''"))
			pojo.setDan("none");
		else
			pojo.setDan(result[21].substring(1).split("'")[0]);
		if (result[22].equals("''"))
			pojo.setShuang("none");
		else
			pojo.setShuang(result[22].substring(1).split("'")[0]);
		if (result[23].equals("''"))
			pojo.setUnknownAttribute5("none");
		else
			pojo.setUnknownAttribute5("unknown");
		if (result[24].equals("''"))
			pojo.setUnknownAttribute6("none");
		else
			pojo.setUnknownAttribute6("unknown");
		if (result[25].equals("''"))
			pojo.setUnknownAttribute7("none");
		else
			pojo.setUnknownAttribute7("unknown");
		if (result[26].equals("''"))
			pojo.setUnknownAttribute8("none");
		else
			pojo.setUnknownAttribute8("unknown");
		if (result[27].equals("''"))
			pojo.setUnknownAttribute9("none");
		else
			pojo.setUnknownAttribute9("unknown");
		if (result[28].equals("''"))
			pojo.setUnknownAttribute10("none");
		else
			pojo.setUnknownAttribute10("unknown");
		if (result[29].equals("''"))
			pojo.setUnknownAttribute11("none");
		else
			pojo.setUnknownAttribute11("unknown");
		if (result[30].equals("''"))
			pojo.setUnknownAttribute12("none");
		else
			pojo.setUnknownAttribute12("unknown");
		if (result[31].equals("''"))
			pojo.setInRangQiuPanKouHalfTime("none");
		else
			pojo
					.setInRangQiuPanKouHalfTime(result[31].substring(1).split(
							"'")[0]);
		if (result[32].equals("''"))
			pojo.setHkRangQiuPanKouHalfTime("none");
		else
			pojo
					.setHkRangQiuPanKouHalfTime(result[32].substring(1).split(
							"'")[0]);
		if (result[33].equals("''"))
			pojo.setShangPanHalfTime("none");
		else
			pojo.setShangPanHalfTime(result[33].substring(1).split("'")[0]);
		if (result[34].equals("''"))
			pojo.setXiaPanHalfTime("none");
		else
			pojo.setXiaPanHalfTime(result[34].substring(1).split("'")[0]);
		if (result[35].equals("''"))
			pojo.setInDaXiaoPanKouHalfTime("none");
		else
			pojo
					.setInDaXiaoPanKouHalfTime(result[35].substring(1).split(
							"'")[0]);
		if (result[36].equals("''"))
			pojo.setHkDaXiaoPanKouHalfTime("none");
		else
			pojo
					.setHkDaXiaoPanKouHalfTime(result[36].substring(1).split(
							"'")[0]);
		if (result[37].equals("''"))
			pojo.setDaQiuHalfTime("none");
		else
			pojo.setDaQiuHalfTime(result[37].substring(1).split("'")[0]);
		if (result[38].equals("''"))
			pojo.setXiaoQiuHalfTime("none");
		else
			pojo.setXiaoQiuHalfTime(result[38].substring(1).split("'")[0]);
		if (result[39].equals("''"))
			pojo.setHomeTeamWinHalfTime("none");
		else
			pojo.setHomeTeamWinHalfTime(result[39].substring(1).split("'")[0]);
		if (result[40].equals("''"))
			pojo.setHomeTeamLoseHalfTime("none");
		else
			pojo.setHomeTeamLoseHalfTime(result[40].substring(1).split("'")[0]);
		if (result[41].equals("''"))
			pojo.setHomeTeamDrawHalfTime("none");
		else
			pojo.setHomeTeamDrawHalfTime(result[41].substring(1).split("'")[0]);

		pojo.setGameId(computeMD5(pojo.getHomeTeamName()
				+ pojo.getCustTeamName() + pojo.getGameDate()
				+ pojo.getGameTime()));
		return pojo;
	}

	public static String computeMD5(String inStr) {
		
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			
			e.printStackTrace();
			return "";
		}
		char[] charArray = inStr.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; i++)
			byteArray[i] = (byte) charArray[i];

		byte[] md5Bytes = md5.digest(byteArray);

		StringBuffer hexValue = new StringBuffer();

		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16)
				hexValue.append("0");
			hexValue.append(Integer.toHexString(val));
		}

		return hexValue.toString();
	}
	
	/**
	 * _a[0] = ['','TSL','TSL (TURKEY SUPER LEAGUE)','200722998','-120','4/2/2007 9:00:00 AM','9:00PM','True','Erciyesspor (H)','Trabzonspor','0','0','0.770','1.145','1.5','1.5','1.160','0.760','0 - 0','','','','','','','','','','','','','','','','','','','1st Half','#000099','0','0'];
	 * 空值在设置时会设置为"none", 参见tirm函数
	 * @param item
	 * @return
	 */
	public static MansionGQ getMansionGQ(String item)
	{
		String[] r = item.split(",");
		MansionGQ m = new MansionGQ();
		
		m.setHomeName(trim(r[8]));
		m.setRoadName(trim(r[9]));
		
		String league = trim(r[2]);
		if(league.contains("(") && league.contains(")"))
			league = league.substring(league.indexOf("(") + 1, league.indexOf(")"));
		m.setLeagueName(league);//TURKEY SUPER LEAGUE
		m.setDatetime(getGQTime(trim(r[5])));//4/2/2007 9:00:00 AM
		m.setRqFlag(trim(r[7]));//True     True是主让客, False是客让主
		m.setRqPankou(trim(r[11]));
		m.setRqHome(trim(r[12]));
		m.setRqRoad(trim(r[13]));
		m.setDxPankou(trim(r[15]));
		m.setDxHome(trim(r[16]));//1.160
		m.setDxRoad(trim(r[17]));
		m.setCurrentScore(trim(r[18]));//0 - 0
		
		m.setRqPankouHalf(trim(r[20]));
		m.setRqHomeHalf(trim(r[21]));
		m.setRqRoadHalf(trim(r[22]));
		m.setRqFlagHalf(trim(r[7]));
		m.setDxPankouHalf(trim(r[24]));
		m.setDxHomeHalf(trim(r[25]));//none
		m.setDxRoadHalf(trim(r[26]));
		
		m.setId(computeMD5(m.getHomeName() + m.getRoadName() + m.getDatetime()));
		
		return m;
	}
	private static String trim(String r)
	{
		if("''".equals(r))return "none";
		return r.substring(1, r.length()-1);
	}
	private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static DateFormat f4gq = new SimpleDateFormat("M/d/yyyy h:mm:ss a", Locale.US);
	/**传入参数格式应为 "M/d/yyyy h:mm:ss a" */
	private static String getGQTime(String s)
	{
		String r = null;
		try
		{
			 r = format.format(f4gq.parse(s));
		} catch (ParseException e)
		{
			logger.warn("接水后在处理日期时出错, mansion的两个日期格式分别应为: '4/2/2007 9:00:00 AM','9:00PM'");
			r = "2007-04-03 03:21:00";
			e.printStackTrace();
		}
		return r;
	}
	
	public static void main(String[] args)
	{
		String odd = "_a[0] = ['','TSL','TSL (TURKEY SUPER LEAGUE)','200722998','-120','4/2/2007 9:00:00 AM','9:00PM','True','Erciyesspor (H)','Trabzonspor','0','0','0.770','1.145','1.5','1.5','1.160','0.760','0 - 0','','','','','','','','','','','','','','','','','','','1st Half','#000099','0','0'];";
		MansionGQ gq = Util.getMansionGQ(odd);
		System.out.println(gq.getCurrentScore());
		System.out.println(gq.getDatetime());
		System.out.println(gq.getDxHome());
		System.out.println(gq.getLeagueName());
		System.out.println(gq.getDxHomeHalf());
	}
}
